// 对后台返回的json对象进行扩展
//{
//  "messages" : [ {
//    "codes" : [ "system.resource.update.success" ],
//    "objectName" : "resource",
//    "action" : "continue",
//    "message" : "修改资源成功.",
//    "code" : "system.resource.update.success"
//  }, {
//    "codes" : [ "名称不正确。" ],
//    "objectName" : "resource",
//    "action" : "continue",
//    "field" : "name",
//    "rejectedValue" : "库存统计",
//    "message" : "名称不正确。",
//    "code" : "名称不正确。"
//  }, {
//    "codes" : [ "系统出现错误" ],
//    "value" : {
//      "id" : 16,
//      "type" : 1,
//      "name" : "库存统计",
//      "sequence" : 0,
//      "level" : 0,
//      "showPos" : 0,
//      "permission" : "",
//      "description" : "",
//      "entry" : "UUU",
//      "display" : 1
//    },
//    "action" : "continue",
//    "code" : "系统出现错误"
//  } ],
//  "messageCount" : 3
//}
function extendMessages(msgs)
{
	jQuery.extend(msgs, new Messages(msgs));
	return msgs;
};

// 所有消息
function Messages(data)
{
	jQuery.extend(this, data);
	this.msg = null;
	this.types= ["alert","success","error","warning","information"];
	this.init();
	jQuery.extend(this.msg, new Message(this.msg));
};
// 单条消息
function Message(data){
	jQuery.extend(this, data);
	this.type = null;
	this.init();
};

Message.prototype.init = function()
{
	var codes =this.code.split("\.");
	this.type = this.getType();
};

Message.prototype.getType = function()
{
	var codes = this.code.split("\.");
	if(codes.length>0 && codes[codes.length-1])
	{
		var type = codes[codes.length-1];
		if(type=="success"
		  || type =="error"
		  || type =="warning"
		  || type =="alert") return type;
		
		if(type=="infor")      return "information";
		if(type=="fatal")      return "error";
	}
	return "alert";
};

Messages.prototype.init= function()
{
	if(this.messages && this.messages.length>0)
    {
		 this.msg =  this.messages[0];

    }
};
// 取得第一条消息
Messages.prototype.firstMessage = function()
{
	 if(this.messages && this.messages.length>0)
     {
		 return this.messages[0];
     }
};
// alert - success - error - warning - information
Messages.prototype.getType = function(code)
{
	var codes = code.split("\.");
	if(codes.length>0 && codes[codes.length-1])
	{
		var type = codes[codes.length-1];
		if(type=="success"
		  || type =="error"
		  || type =="warning"
		  || type =="alert") return type;
		
		if(type=="infor")      return "information";
		if(type=="fatal")      return "error";
	}
	return "alert";
};

//取得第一条消息
Messages.prototype.isAllSuccess = function()
{
	 var cnt = this.messages.length;
	 for(var i=0;i<cnt;i++){
		 var msg = this.messages[i];
		 var codes =msg.code.split("\.");
		 if(codes[codes.length-1])
		 {
			var type = this.getType(codes[codes.length-1]);
			if(type!="success") return false;
		 } 
	 }
	 return true;
};

Messages.prototype.clearTips = function($container){
	$container.find("input,select").each(function(){
		var tip = $(this);
		if(!$(this).is(":visible")){
			// 取得同级下一个元素
			tip = $(this).next();
			//if(tip.length==0) 
		}
		if(tip.length>0){
			tip.qtip("destroy");
			tip.removeClass("error");
		}
	});
}

Messages.prototype.show = function($container,options){
	var defaultoptions = {
			overwrite: true,  
			/*
			show: {  
				event: false,  
				ready: true  
			},
			*/
			/*hide: false,  */
			position: {
				my:"top center",
				at:"bottom center"
				//viewport: $(window)  
			 }
	}
	
	var len = this.messages.length;
	// clear all tip
	// alert - success - error - warning - information
	var msgcontainer = {
			alert:{
				container:$("<ul></ul>"),
				count:0
			},
			success:{
				container:$("<ul></ul>"),
				count:0
			},
			error:{
				container:$("<ul></ul>"),
				count:0
			},
			warning:{
				container:$("<ul></ul>"),
				count:0
			},
			information:{
				container:$("<ul></ul>"),
				count:0
			}
	}
	
	for(var i=0;i<len;i++)
	{
		var msg = this.messages[i];
		var opt = {content: msg.message};
		$.extend(opt,defaultoptions,options);
		
		if($container && msg.field)
		{
			var tip = $container.find("input[name="+msg.field+"],select[name="+msg.field+"]")
			if(!tip.is(":visible")){
				// 取得同级下一个元素
				tip = tip.next();
			}
			if(tip.length>0){
				tip.qtip(opt).qtip('option', 'content.text', msg.message);
				tip.addClass("error");
			}
			
		}else{ // normal message
			var type = this.getType(msg.code);
			$("<li></li>").append(msg.message).appendTo(msgcontainer[type].container);
			msgcontainer[type].count++;
		}
	}
	for(var i=0;i<this.types.length;i++){
		var type = this.types[i];
		var contain = msgcontainer[type];
		if(contain.count>0){
			noty({
				text:contain.container.html(),
				type:type,
				timeout:2000
		    });
		}
	}
}
 
Messages.prototype.show2 = function()
{
	// build message
	var $msg =$("<ul></ul>");
	var len = this.messages.length;
	for(var i=0;i<len;i++)
	{
		var msg = this.messages[i];
		if(msg.field)
		{
			$("<li></li>").append(msg.rejectedValue).append(msg.message).appendTo($msg);
		}else{
			$("<li></li>").append(msg.message).appendTo($msg);
		}
	}
	if(len>0)
	{ 
		// show with noty
		if(!this.isAllSuccess())
	    {
			noty({
					text:$msg.html(),
					type:"error",
					timeout:2000
			 });
	    }else{
	    	noty({
				text:$msg.html(),
				type:"success",
				timeout:2000
		  });
	    }
	}
};
